package com.bltec.emergency.controller.mgrController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.User;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 获得所有用户
     */
    @RequestMapping("getAllUser")
    public ResponseJson getUser() {
        try {
            List<User> users = userService.getAll();
            users.removeIf(role -> role.getId() == 1);
            return new ResponseJson(Constant.OK, JSON.toJSON(users));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加用户
     */
    @RequiresPermissions("user:insert")
    @RequestMapping("addUser")
    public ResponseJson addUser(String username, String password, String sex, String email, Long roleId) {
        try {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setSex(sex);
            user.setEmail(email);
            user.setRoleId(roleId);
            user.setEnable(true);
            user.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            user.setCreateTime(DateUtil.dateNow());
            userService.add(user);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("user:update")
    @RequestMapping("updateUser")
    public ResponseJson updateUser(Long id, String username, String tel, String phone, Integer departmentId, String position, String sex, String email, Long roleId) {
        try {
            User user = userService.getUserById(id);
            List<String> usernameList = userService.selectAllUsername();
            user.setUsername(username);
            user.setTel(tel);
            user.setPhone(phone);
            user.setDepartmentId(departmentId);
            user.setPosition(position);
            user.setSex(sex);
            user.setEmail(email);
            user.setRoleId(roleId);
            user.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            user.setUpdateTime(DateUtil.dateNow());
            if (!usernameList.contains(username) && userService.updateUserById(user)) {
                return new ResponseJson(Constant.OK, JSON.toJSON(user));
            } else {
                return new ResponseJson(Constant.FAIL_USER_LOCK, "该用户名已存在！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除用户
     */
    @RequiresRoles("superManger")
    @RequiresPermissions("user:delete")
    @RequestMapping("deleteUser")
    public ResponseJson setDisable(Long id) {
        if (null != userService.getUserById(id)) {
            if (userService.deleteUserById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
